package com.henchman.samples;

import java.io.BufferedOutputStream;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import javax.xml.transform.TransformerConfigurationException;
import javax.xml.transform.sax.SAXTransformerFactory;
import javax.xml.transform.sax.TransformerHandler;
import javax.xml.transform.stream.StreamResult;

import org.tigris.subversion.javahl.ChangePath;
import org.tigris.subversion.javahl.ClientException;
import org.tigris.subversion.javahl.LogMessageCallback;
import org.tigris.subversion.javahl.Revision;
import org.tigris.subversion.javahl.SVNClientInterface;
import org.tmatesoft.svn.core.ISVNLogEntryHandler;
import org.tmatesoft.svn.core.SVNException;
import org.tmatesoft.svn.core.SVNLogEntry;
import org.tmatesoft.svn.core.SVNURL;
import org.tmatesoft.svn.core.auth.ISVNAuthenticationManager;
import org.tmatesoft.svn.core.wc.ISVNOptions;
import org.tmatesoft.svn.core.wc.SVNLogClient;
import org.tmatesoft.svn.core.wc.SVNRevision;
import org.tmatesoft.svn.core.wc.SVNWCUtil;
import org.tmatesoft.svn.core.wc.xml.SVNXMLLogHandler;

/**
 * 
 * @author Raghunathan K Semburakkiannan
 *
 */
public class DisplayContent {

	String userId = "anonymous", password = "anonymous";
	SvnContext context = new SvnContext(userId, password);
	SVNClientInterface client = context.allocClient();

	String url = "http://svn.svnkit.com/repos/svnkit/trunk/README.txt";
	String rootURL = "http://svn.svnkit.com/repos/svnkit/trunk";
	Revision rev = Revision.HEAD;

	public void dispFile() {
		OutputStream os = new ByteArrayOutputStream();

		try {
			client.streamFileContent(url, rev, rev, 8192, os);
		} catch (ClientException e) {
			e.printStackTrace();
		}
		System.out.println(os.toString());
	}
	
	public void dispLog()  throws SVNException, FileNotFoundException {
		ISVNAuthenticationManager authManager = SVNWCUtil.createDefaultAuthenticationManager( userId , password);
		ISVNOptions options = SVNWCUtil.createDefaultOptions(true);
		
		SVNLogClient svnLogClient = new SVNLogClient(authManager, options);
		
		OutputStream os = new BufferedOutputStream(new FileOutputStream(new File("C:\\workspace\\review-henchman\\temp\\test.xml")));
		
        TransformerHandler th = createTransformerHandler();
        th.setResult(new StreamResult(os));
        SVNXMLLogHandler logHandler = new SVNXMLLogHandler(th);
        logHandler.startDocument();
        svnLogClient.doLog(SVNURL.parseURIEncoded(rootURL), null, SVNRevision.UNDEFINED, SVNRevision.HEAD, SVNRevision.create(1L), false, true, 20, logHandler);
        logHandler.endDocument();
		
	}

	public static void main(String[] args) throws IOException, SVNException, ClientException {
		DisplayContent disp = new DisplayContent();

		System.out.println("The file content");
		//disp.dispFile();
		
		System.out.println("File browsing");
		//disp.dispLog();
		
		System.out.println("File browsing");
		disp.dispUsingTigris();
		

	}
	
	public void dispUsingTigris() throws FileNotFoundException, ClientException {
		final List<SvnRevisionInfo> revisions = new ArrayList<SvnRevisionInfo>(10);
		String[] STD_PROPS = new String[] {"svn:date", "svn:author", "svn:log"};
		
		//client.logMessages(rootURL, Revision.HEAD, RevisionRange[] ranges, boolean stopOnCopy, boolean discoverPath, boolean includeMergedRevisions, String[] revProps, long limit, LogMessageCallback callback) 
		
        client.logMessages(rootURL, Revision.HEAD, Revision.HEAD, new Revision.Number(0), false, true, false,
        		STD_PROPS , 5, new LogMessageCallback() {
            public void singleMessage(ChangePath[] changePaths, long revnum, Map revpropMap, boolean hasChildren) {
                SvnRevisionInfo revInfo = new SvnRevisionInfo(revnum, revpropMap, changePaths);
                revisions.add(revInfo);
            }
        });
        
        for(SvnRevisionInfo revInfo:revisions) {
        	System.out.println("Revision # :"+revInfo.getRevisionNumber()+" Author:"+revInfo.getAuthor()+" Date:"+revInfo.getDate());
        	System.out.println("Path:");
        	for(ChangePath changePath: revInfo.getChangedPaths()){
        		System.out.println("===>"+changePath.getPath());
        	}
        }
	}
	
    private static TransformerHandler createTransformerHandler() {
        try {
            return ((SAXTransformerFactory) SAXTransformerFactory.newInstance()).newTransformerHandler();
        } catch (TransformerConfigurationException e) {
            throw new Error(e); // impossible
        }
    }


}
